$(document).ready(function() {
    $("#fileBase").hide();
    hideShowNormalValues();
    showMoreInfo($("#div_"), "imgInfo");
    showMoreInfo($("#div_zip"), "imgInfoZip");
    inicializarTodaFechaNV();
});

function inicializarTodaFechaNV(){
    $(".nvDatepicker").each(function(i,v){
        var idDate = "#" + v.id;
        var idAlt = "#alt" + v.id;
        initializeDate(idDate, idAlt);
    });
}

function setEmptyDatesNV(){
    $(".nvDatepicker").each(function(i,v){
        if($(v).val() == ''){
            var idAlt = "#alt" + v.id;
            $(idAlt).val('');
        }
    });
}

function getPaisByProtocolo(protocoloid, protocolo, url){

   $("#protocolo").val(protocolo);
    if(protocoloid >= 0){
        $.ajax({
            traditional: true,
            type: "POST",
            url: url,
            data: { "protocoloid": protocoloid},
            success: function(result)
            {
                $("#protocolo").parent().parent().after(result);
            }
        });
    }
}

function isOtherVendorType(value){
    if (value == -2){
        $("#otherVendorType").parent().parent().show()
    }
    else{
        $("#otherVendorType").parent().parent().hide()

    }
}

function beforeSaveVendor(){

    if ($('#type option:selected').val() == -2){
        $("#typeDescripcion").val($("#otherVendorType").val());
    }

    if ($('#type option:selected').val() == -1){
        $("#typeDescripcion").val('');
    }

}

function validate() {
    $("#formEdit")
        .validate(
        {

            rules : {
                'name' : "required",
                'category' : "required",
                'address.address' : "required",
                'countries.country' : "required",
                'regions.region' : "required",
                'cities.city' : "required",
                'address.zipCode' : "number",
                'address.email' : {
                    required : true,
                    email : true
                },
                'type.id' :  "required",
                'contactPerson.email' : { required : true,
                                          email : true}

            }
        })

/*    $(".nvtypeSelect").each(function(index,value){
        if( $(".nvDatepicker")[index].value != "" || $(".fileClass")[index].value != ""){
            $(value).rules("add", {
                required:true
            });
        }
        else{
            if(value.value == ""){
                $(value).rules("remove",'required');
            }
        }
    });

    $(".nvDatepicker").each(function(index,value){
        if( $(".nvtypeSelect")[index].value != "" || $(".fileClass")[index].value != ""){
            $(value).rules("add", {
                required:true
            });
        }
        else{
            if(value.value == ""){
                $(value).rules("remove",'required');
            }
        }
    });*/

    var cantNombres = $(".fileNameClass").length;
    if($("#formEdit").valid()){
        $(".fileClass").each(function(i,v){
            if(v.value != ""){
                var tienenFile = $("#tienenFile").val();
                var ind = parseInt(cantNombres,10) + parseInt(i,10);
                $("#tienenFile").val(tienenFile + ind + ", ");
            }
        });
    }

}

function fillCountryCode(countryId, url){
    var phone = $("#phone").val();
    if(countryId >= 0 && phone == ""){
        $("#phone").parent().parent().remove();
        $.ajax({
            traditional: true,
            type: "POST",
            url: url,
            data: { "countryId": countryId},
            success: function(result)
            {
                $("#fax").parent().parent().after(result);
                $("#addressPhone").val($("#phone").val());
                showMoreInfo($("#div_"), "imgInfo");
            }
        });
    }
}

function hideShowNormalValues(){
    var selValue = $("#type").val();
    var text = $("#type option:selected").text();
    if (text == "Laboratory"){
        $('#table_normal_values').show();
    }
    else{
        $('#table_normal_values').hide();
    }
}

function addNormalValues(tableID){
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    var cant = document.getElementById("cantNv");
    var val = parseInt(cant.value,10) + parseInt(1,10);
    cant.value = val;

    var ultimoId = document.getElementById("ultimoIdNv");

    for(var k = ultimoId.value; k>0; k--){
        if (document.getElementById("delNvCell" + k) != null)
            break;
    }

    var newId = parseInt(ultimoId.value,10) + parseInt(1,10);
    ultimoId.value = newId;

    var idAfter = '#delNvCell' + k;

    var row5 = $('#delNvCell' + k).parent().clone()[0];
    row5.cells[1].id="delNvCell"+newId;
    row5.cssClass="trNuevoDocumento";
    $(row5).css("display", "table-row");
    $(idAfter).parent().after(row5);

    var row4 = $('#expDate' + k).parent().parent().clone()[0];
    row4.cells[1].children[1].name="vendorcentral.normalValues[" + newId + "].expirationDate";
    row4.cells[1].children[0].value="";
    row4.cells[1].children[1].value="";
    row4.cells[1].children[0].id="expDate" + newId;
    row4.cells[1].children[1].id="altexpDate" + newId;
    row4.cells[1].children[0].cssClass="nvDatepicker";
    $(row4).removeClass("trEspacioAbajo");
    $(row4.cells[1].children[0]).removeAttr("disabled");
    if(row4.cells[1].children.length == 3){
        row4.cells[1].children[2].name="";
        row4.cells[1].children[2].value="";
    }
    $(idAfter).parent().after(row4);
    initializeDate("#expDate" + newId, "#altexpDate" + newId);

    var row3 = $('#fileBaseCell').parent().parent().clone()[0];
    row3.cells[1].children[0].name="files";
    row3.cells[1].children[0].id="file" + newId;
    row3.cells[1].children[0].value="";
    $(row3.cells[1].children[0]).addClass("fileClass");
    $(row3).show();
    $(idAfter).parent().after(row3);

    var row2 = $('#nvtype' + k).parent().parent().clone()[0];
    row2.cells[1].children[0].name="vendorcentral.normalValues[" + newId + "].type.id";
    row2.cells[1].children[0].id="nvtype" + newId;
    row2.cells[1].children[0].value="";
    $(row2.cells[1].children[0]).addClass("nvtypeSelect");
    $(row2.cells[1].children[0]).removeAttr("disabled");
    if(row2.cells[1].children.length == 2){
        row2.cells[1].children[1].name="";
        row2.cells[1].children[1].value="";
    }
    $(idAfter).parent().after(row2);

}

function deleteNormalValues(tableId, fila){
    var table = document.getElementById(tableId);
    var cant = document.getElementById("cantNv");

    var rows = table.getElementsByTagName('tr');
    var size = rows.length;
    for(var j = 0; j < rows.length; j++ ){
        if(rows[j].id == fila[0].id && rows[j].cells[1].id == fila[0].cells[1].id){
            break;
        }
    }

    if(cant.value == 1){
        var borrados = 0;
        var x = j - 1;
        while(borrados < 3){
            if(rows[x].cells[1] != null){
                rows[x].cells[1].children[0].value="";
                borrados++;
            }
            x--;
        }
        return;
    }

    var borrados = 0;
    var x = j;
    while(borrados < 4){
        rows[x].parentNode.removeChild(rows[x]);
        if(rows[x].cells[1] != null){
            borrados++;
        }
        x--;
    }

    var val = parseInt(cant.value,10) - parseInt(1,10);
    cant.value = val;
}


function deleteExternalValidation(tableId, fila){
    var table = document.getElementById(tableId);
    var cant = document.getElementById("cantEv");

    var rows = table.getElementsByTagName('tr');
    var size = rows.length;
    for(var j = 0; j < rows.length; j++ ){
        if(rows[j].id == fila[0].id && rows[j].cells[1].id == fila[0].cells[1].id){
            break;
        }
    }

    if(cant.value == 1){
        var borrados = 0;
        var x = j - 1;
        while(borrados < 2){
            if(rows[x].cells[1] != null){
                rows[x].cells[1].children[0].value="";
                borrados++;
            }
            x--;
        }
        return;
    }

    var borrados = 0;
    var x = j;
    while(borrados < 3){
        rows[x].parentNode.removeChild(rows[x]);
        if(rows[x].cells[1] != null){
            borrados++;
        }
        x--;
    }

    var val = parseInt(cant.value,10) - parseInt(1,10);
    cant.value = val;
}

function addExternalValidation(tableID){
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    var cant = document.getElementById("cantEv");
    var val = parseInt(cant.value,10) + parseInt(1,10);
    cant.value = val;

    var ultimoId = document.getElementById("ultimoIdEv");

    for(var k = ultimoId.value; k>0; k--){
        if (document.getElementById("delEvCell" + k) != null)
            break;
    }

    var newId = parseInt(ultimoId.value,10) + parseInt(1,10);
    ultimoId.value = newId;

    var idAfter = '#delEvCell' + k;

    var row5 = $('#delEvCell' + k).parent().clone()[0];
    row5.cells[1].id="delEvCell"+newId;
    row5.cssClass="trNuevoDocumento";
    $(idAfter).parent().after(row5);

    var row4 = $('#evExpDate' + k).parent().parent().clone()[0];
    row4.cells[1].children[1].name="vendorcentral.externalValidations[" + newId + "].expirationDate";
    row4.cells[1].children[0].value="";
    row4.cells[1].children[1].value="";
    row4.cells[1].children[0].id="evExpDate" + newId;
    row4.cells[1].children[1].id="altevExpDate" + newId;
    $(idAfter).parent().after(row4);
    initializeDate("#evExpDate" + newId,"#altevExpDate" + newId );

    var row2 = $('#evtype' + k).parent().parent().clone()[0];
    row2.cells[1].children[0].name="vendorcentral.externalValidations[" + newId + "].type";
    row2.cells[1].children[0].id="evtype" + newId;
    row2.cells[1].children[0].value="";
    $(idAfter).parent().after(row2);

}

function baShowHide(){
}

function updateDatepickers(){
    $('.nvDatepicker').each(function(i,v){
        var idAlt = "#alt" + v.id;
        $(v).removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/M/y', altField : idAlt, altFormat: 'dd/mm/y'});
    });
    $('.datepicker').each(function(i,v){
       var idAlt = "#alt" + v.id;
       $(v).removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/M/y', altField : idAlt, altFormat: 'dd/mm/y'});
   });
}